home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 05 / 6 / DISK0564.ZIP / SOURCE.ARC / CLEAN.C < prev    next >
C/C++ Source or Header  |  1988-07-09  |  2KB  |  59 lines

  1. /* this program removes all control characters from a file (except
  2.    for carriage return, line feed, form feed, tab, and backspace, which
  3.    it leaves alone */
  4.    
  5. /* It reads from a file if a file name is specified, or from the
  6.    standard input if no file name is given.  It writes to the standard
  7.    output */
  8.  
  9. /* by Jon Dart, 3012 Hawthorn St., San Diego, CA 92104 */
  10. /* version 1.0, 16-Nov-86 */
  11.  
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14.  
  15. #define PATHSIZE     65    /* max size of DOS pathname + 1 */
  16. #define BUFSIZE        16384   /* input buffer size */
  17. #define EOF        -1
  18. #define CR        (unsigned char) '\015'
  19. #define LF        (unsigned char) '\012'
  20. #define FF        (unsigned char) '\014'
  21. #define BKS             (unsigned char) '\010'
  22. #define TAB             (unsigned char) '\011'
  23. #define CTRLZ           (unsigned char) '\032'
  24.  
  25. extern char *malloc();
  26.  
  27. main(argc,argv)
  28. int argc; char **argv;
  29. {
  30.     FILE *infd;    
  31.     register int c;
  32.     unsigned char *buffer;
  33.  
  34.     buffer = (unsigned char *)  malloc(BUFSIZE);
  35.  
  36.     if (argc>=2) {
  37.         infd = fopen(argv[1],"r");
  38.         if (infd == NULL) {
  39.             fprintf(stderr,"\nCan't open: %s\n",argv[1]);
  40.             exit(1);
  41.         }
  42.     }
  43.     else /* read from std input */
  44.         infd = stdin;
  45.     setbuf(infd,buffer);
  46.     
  47.     while ((c=getc(infd)) != EOF) {
  48.         c = c & 0177;   /* strip hi bit */
  49.     if (c==CTRLZ) 
  50.         break;
  51.     else if ( (c>=32 && c!=0177) 
  52.            || (c==CR) || (c==LF) || (c==BKS) || (c==TAB) || (c==FF) ) 
  53.                putc(c,stdout);
  54.     }
  55.     if (ferror(infd)) fprintf(stderr,"clean: error in reading file\n");
  56.     if (ferror(stdout)) fprintf(stderr,"clean: error in writing file\n");
  57.     fclose(infd);
  58. }
  59.